package com.lanou.sfu.songforu.music_library_fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;
import com.lanou.sfu.songforu.adapter.RankClassAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.datas.MusicListDatas;
import com.lanou.sfu.songforu.db.SongListEntity;
import com.lanou.sfu.songforu.entity.RankClassEntity;
import com.lanou.sfu.songforu.singleton.VolleySingleton;
import com.lanou.sfu.songforu.tools.ListViewForScrollView;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;

import java.util.ArrayList;
import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;


/**
 * Created by dllo on 15/10/22.
 */
public class RankClassFragment extends BaseFragment implements AdapterView.OnItemClickListener, RankClassAdapter.OnClickToShareLinstener {
    private ListViewForScrollView listView;
    private Context context;
    private VolleySingleton requestQueue;
    private StringRequest request;
    private List<SongListEntity> rankClassList;
    private ImageView imageView;
    private ImageButton playAll,share;
    private TextView updataTime;
    private UMSocialService mController;
    private View v;
    private TextView total;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.activity_rank_class,null);
        init();
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        // Qzone分享
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(getActivity(),getString(R.string.qQ_appID),getString(R.string.qQ_appSecret));
        qZoneSsoHandler.addToSocialSDK();

        // qq分享
        UMQQSsoHandler qqssoH = new UMQQSsoHandler(getActivity(), getString(R.string.qQ_appID),getString(R.string.qQ_appSecret));
        qqssoH.addToSocialSDK();

        // 设置分享内容
        QQShareContent qqcontent = new QQShareContent();
        qqcontent.setShareContent(context.getString(R.string.RANK_CLASS_SHARE_CONTENT));
        mController.setShareMedia(qqcontent);
        // 获得上页面传的类型字段 与排行榜类型url拼接
        Bundle bundle = getArguments();
        String name = bundle.getString(context.getString(R.string.name));
        int type = bundle.getInt(context.getString(R.string.type), 0);
        String url = Url.URL_RANK_CLASS + type;
        listView.setOnItemClickListener(this);
        // 给页面设置工具栏
        final Toolbar toolbar = (Toolbar) v.findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.mipmap.bt_return_nor);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
        toolbar.setTitle(name);
        // 设置工具栏标题
        CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) v.findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setTitle(name);
        collapsingToolbar.setContentScrimColor(0xf48852);
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mController.openShare(getActivity(), false);
            }
        });
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                final RankClassEntity rankClassEntity = gson.fromJson(response,RankClassEntity.class);
                rankClassList = rankClassEntity.getSong_list();
                RankClassAdapter adapter = new RankClassAdapter(context,rankClassList);
                adapter.setOnClickToShareLinstener(RankClassFragment.this);
                listView.setAdapter(adapter);
                Picasso.with(context).load(rankClassEntity.getBillboard().getPic_s640()).into(imageView);
                updataTime.setText(rankClassEntity.getBillboard().getUpdate_date());
                total.setText(rankClassList.size() + "");
                playAll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ArrayList<MusicListDatas> lists = new ArrayList<>();
                        for (SongListEntity entity : rankClassList) {
                            MusicListDatas musicList = new MusicListDatas();
                            musicList.setName(entity.getTitle());
                            musicList.setSinger(entity.getAuthor());
                            musicList.setSongId(entity.getSong_id());
                            musicList.setSingerImage(entity.getPic_big());
                            musicList.setFrom(getString(R.string.NET));
                            lists.add(musicList);
                        }
                        MainActivity.play(lists, 0);
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context, getString(R.string.ifNet),Toast.LENGTH_LONG).show();
            }
        });
        requestQueue.addQueue(request);




    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ArrayList<MusicListDatas> lists = new ArrayList<>();
        for (SongListEntity entity : rankClassList) {
            MusicListDatas musicList = new MusicListDatas();
            musicList.setName(entity.getTitle());
            musicList.setSinger(entity.getAuthor());
            musicList.setSongId(entity.getSong_id());
            musicList.setSingerImage(entity.getPic_big());
            musicList.setFrom(getString(R.string.NET));
            lists.add(musicList);
        }
        MainActivity.play(lists, position);
    }

    private void init(){
        imageView = (ImageView) v.findViewById(R.id.backdrop);
        playAll = (ImageButton) v.findViewById(R.id.playAll_imageButton);
        updataTime = (TextView) v.findViewById(R.id.rank_class_updata_time);
        listView = (ListViewForScrollView) v.findViewById(R.id.activity_rank_class_listView);
        share = (ImageButton) v.findViewById(R.id.rank_class_all_share_imageButton);
        context = getActivity();
        mController = UMServiceFactory.getUMSocialService(getString(R.string.UMSocialService));
        requestQueue = VolleySingleton.getInstance(context);
        total = (TextView) v.findViewById(R.id.rank_class_total);
    }

    @Override
    public void onClickToShare(SongListEntity songListEntity, int position) {
        mController.openShare(getActivity(), false);
    }
}
